package gridmapper;

import gridmapper.Constants.Type;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author estell
 */
public class MasterImage extends javax.swing.JPanel implements KeyListener, MouseListener {

	private BufferedImage imag;
	private MapPanel mapLink;
	private Constants.Type selectedType;
	private String subtype = "a";

	public String getSubtype() {
		return subtype;
	}

	public void setSubtype(String subtype) {
		this.subtype = subtype;
	}

	public Type getSelectedType() {
		return selectedType;
	}

	public void setSelectedType(Type selectedType) {
		this.selectedType = selectedType;
	}

	public MapPanel getMapLink() {
		return mapLink;
	}

	public void setMapLink(MapPanel mapLink) {
		this.mapLink = mapLink;
	}

	/** Creates new form MasterImage */
	public MasterImage() {
		super();
		initComponents();
		this.addMouseListener(this);
		this.addKeyListener(this);

	}

	public void loadImage(String tmpPath) {
		if (imag == null) {
			try {
				imag = ImageIO.read(new File(tmpPath));

			} catch (IOException ex) {
				Logger.getLogger(MasterImage.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	}

	public BufferedImage getImag() {
		return imag;
	}

	public void setImag(BufferedImage imag) {
		this.imag = imag;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 570, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
	@Override
	public void paintComponent(Graphics g) {
		if (imag != null) {
			super.paintComponent(g);
			g.drawImage(imag, 0, 0, imag.getWidth(), imag.getHeight(), null);
		}
	}

	public void keyTyped(KeyEvent e) {
		//GNDN
	}

	public void keyPressed(KeyEvent e) {
		//GNDN
	}

	public void keyReleased(KeyEvent e) {
		//GNDN
	}

	public void mouseClicked(MouseEvent e) {
		System.out.println("Mouse was clicked on the master map");
		int gridx = (e.getX() / mapLink.getTileWidth());
		int gridy = (e.getY() / mapLink.getTileHeight());

		System.out.println("clicked on space " + gridx + " spaces over, and " + gridy + " spaces down");


		//@TODO:  Hiedous...  MUST FIX!
		if (gridy == Constants.WATER_CURVES) {
			selectedType = Constants.Type.TYPE_UNKNOWN;
		} else if (gridy == Constants.GRASS) {
			selectedType = Constants.Type.TYPE_GROUND;
		} else if (gridy == Constants.DIRT) {
			selectedType = Constants.Type.TYPE_PATH;
		} else if (gridy == Constants.CRACKED) {
			selectedType = Constants.Type.TYPE_UNKNOWN;
		} else if (gridy == Constants.JUNGLE) {
			selectedType = Constants.Type.TYPE_JUNGLE;
		} else if (gridy == Constants.SWAMP) {
			selectedType = Constants.Type.TYPE_UNKNOWN;
		} else if (gridy == Constants.MARSH) {
			selectedType = Constants.Type.TYPE_UNKNOWN;
		} else if (gridy == Constants.ROCKS) {
			selectedType = Constants.Type.TYPE_UNKNOWN;
		} else if (gridy == Constants.RIVER) {
			selectedType = Constants.Type.TYPE_UNKNOWN;
		} else if (gridy == Constants.WATER) {
			selectedType = Constants.Type.TYPE_LIQUID;
		} else if (gridy == Constants.HILLS) {
			selectedType = Constants.Type.TYPE_UNKNOWN;
		} else if (gridy == Constants.TREES) {
			selectedType = Constants.Type.TYPE_FOREST;
		} else if (gridy == Constants.MOUNTAINS) {
			selectedType = Constants.Type.TYPE_UNKNOWN;

		}

		subtype = String.valueOf(gridx);

	}

	public void mousePressed(MouseEvent e) {
		//GNDN
	}

	public void mouseReleased(MouseEvent e) {
		//GNDN
	}

	public void mouseEntered(MouseEvent e) {
		//GNDN
	}

	public void mouseExited(MouseEvent e) {
		//GNDN
	}
}
